﻿from django.http import HttpResponse, HttpResponseRedirect
from django.core.paginator import ObjectPaginator, InvalidPage
from app.addons import models
from app.forum import models as models1
from google.appengine.api import users
from google.appengine.ext import db
from datetime import datetime
from app.functions.funcs import render,split_tags,formate_time,gen_duration,gen_quote
from app.base.auth import LoginUrl,ChkAuth,admin_required,login_required,member_required
from app.functions.postmarkup import render_bbcode
from google.appengine.api import memcache

@member_required
def CreateAddon(request):
    multi_language = memcache.get_multi(['fa_forum_menu_home','fa_forum_menu_dkp','fa_forum_menu_help','fa_forum_menu_admin','fa_forum_createtopic_lang_topic_title',
                                        'fa_forum_second_menu_topic','fa_forum_second_menu_setting',
                                        'fa_forum_createtopic_lang_topic_content',
                                        'fa_forum_createtopic_lang_submit',
                                        'fa_forum_sider_lang_home',
                                        'fa_forum_sider_lang_register',
                                        'fa_forum_sider_lang_create_topic',
                                        'fa_forum_sider_lang_member_info',
                                        'fa_forum_sider_lang_signout',
                                        'fa_forum_sider_lang_signin'])
    greeting = LoginUrl()
    user = users.get_current_user()
    alltags = models.Tags.all()
    if request.method == 'POST':
        try:
            for f,file_info in request.FILES.items():
                screenshot = file_info['content']
        except:
            screenshot = None
        name = request.POST.get('name').decode('utf-8')
        summary = request.POST.get('summary').decode('utf-8')
        author = request.POST.get('author').decode('utf-8')
        version = request.POST.get('version').decode('utf-8')
        catagory = request.POST.get('catagory').decode('utf-8')
        download = request.POST.get('download').decode('utf-8')
        tags1 = request.POST.get('tags').decode('utf-8')
        tags = split_tags(tags1)
        member = models1.Members.all().filter('user =',user)
        member = member[0]
        addon = models.Addons(name=name,
                                summary=summary,
                                author=author,
                                version=version,
                                catagory=catagory,
                                download=download,
                                tags=tags,
                                member=member,
                                screenshot=screenshot,)
        addon.put_addon()
        return HttpResponseRedirect('/addons/')
    payload = dict(quotation=viewquote(),alltags=alltags,greeting=greeting,multi_language=multi_language,page_type='addons',user=user)
    return render('createAddon.html',payload)

@member_required
def EditAddon(request,addon_id):
    multi_language = memcache.get_multi(['fa_forum_menu_home','fa_forum_menu_dkp','fa_forum_menu_help','fa_forum_menu_admin','fa_forum_createtopic_lang_topic_title',
                                        'fa_forum_second_menu_topic','fa_forum_second_menu_setting',
                                        'fa_forum_createtopic_lang_topic_content',
                                        'fa_forum_createtopic_lang_submit',
                                        'fa_forum_sider_lang_home',
                                        'fa_forum_sider_lang_register',
                                        'fa_forum_sider_lang_create_topic',
                                        'fa_forum_sider_lang_member_info',
                                        'fa_forum_sider_lang_signout',
                                        'fa_forum_sider_lang_signin'])
    greeting = LoginUrl()
    user = users.get_current_user()
    alltags = models.Tags.all()
    addon = models.Addons.get_by_id(int(addon_id))
    screenshot = '/addon/pic/%s' %addon_id
    tags = ",".join(["%s" %i for i in addon.tags])
    if request.method == 'POST':
        try:
            for f,file_info in request.FILES.items():
                img_data = file_info['content']
            addon.screenshot = img_data
        except:
            pass
        addon.name = request.POST.get('name').decode('utf-8')
        addon.summary = request.POST.get('summary').decode('utf-8')
        addon.author = request.POST.get('author').decode('utf-8')
        addon.version = request.POST.get('version').decode('utf-8')
        addon.catagory = request.POST.get('catagory').decode('utf-8')
        addon.download = request.POST.get('download').decode('utf-8')
        tags = request.POST.get('tags').decode('utf-8')
        addon.tags = split_tags(tags)
        addon.put_addon()
        return HttpResponseRedirect('/addons/edit/%s' %addon_id)
    payload = dict(quotation=viewquote(),screenshot=screenshot,alltags=alltags,greeting=greeting,addon=addon,tags=tags,multi_language=multi_language,page_type='addons',user=user)
    return render('createAddon.html',payload)

def ViewAddons(request):
    multi_language = memcache.get_multi(['fa_forum_menu_home','fa_forum_menu_dkp','fa_forum_menu_help','fa_forum_menu_admin','fa_forum_view_lang_delete',
                                        'fa_forum_second_menu_topic','fa_forum_second_menu_setting',
                                        'fa_forum_view_lang_edit',
                                        'fa_forum_view_lang_by',
                                        'fa_forum_view_lang_from',
                                        'fa_forum_view_lang_submit',
                                        'fa_forum_sider_lang_home',
                                        'fa_forum_sider_lang_register',
                                        'fa_forum_sider_lang_create_topic',
                                        'fa_forum_sider_lang_member_info',
                                        'fa_forum_sider_lang_signout',
                                        'fa_forum_sider_lang_signin'])
    greeting = LoginUrl()
    user = users.get_current_user()
    keyname = "keytags"
    alltags = models.Tags.get_or_insert(keyname)
    addons = models.Addons.all().order('-post_on')
    payload = dict(quotation=viewquote(),alltags=alltags.alltags,greeting=greeting,addons=addons,user=user,multi_language=multi_language,page_type='addons')
    return render('addons.html', payload)

def viewAddonsBy2(request,viewby,value):
    multi_language = memcache.get_multi(['fa_forum_menu_home','fa_forum_menu_dkp','fa_forum_menu_help','fa_forum_menu_admin','fa_forum_view_lang_delete',
                                        'fa_forum_second_menu_topic','fa_forum_second_menu_setting',
                                        'fa_forum_view_lang_edit',
                                        'fa_forum_view_lang_by',
                                        'fa_forum_view_lang_from',
                                        'fa_forum_view_lang_submit',
                                        'fa_forum_sider_lang_home',
                                        'fa_forum_sider_lang_register',
                                        'fa_forum_sider_lang_create_topic',
                                        'fa_forum_sider_lang_member_info',
                                        'fa_forum_sider_lang_signout',
                                        'fa_forum_sider_lang_signin'])
    greeting = LoginUrl()
    user = users.get_current_user()
    keyname = "keytags"
    alltags = models.Tags.get_or_insert(keyname)
    if viewby == 'catagory':
        addons = models.Addons.all().filter('catagory =', value.decode('utf-8'))
    elif viewby == 'author':
        addons = models.Addons.all().filter('auhtor =', value.decode('utf-8'))
    elif viewby == 'tag':
        addons = []
        for addon in models.Addons.all().order('-post_on'):
            if value.decode('utf-8') in addon.tags:
                addons.append(addon)
    payload = dict(quotation=viewquote(),alltags=alltags.alltags,greeting=greeting,addons=addons,user=user,multi_language=multi_language,page_type='addons')
    return render('addons.html', payload)



def DelAddon(request, addon_id):
    addon = models.Addons.get_by_id(int(addon_id))
    user = users.get_current_user()
    if user == addon.member.user:
        addon.delete()
    else:
        pass
    return HttpResponseRedirect('/addons/')
    
def getpic(request,addon_id):
    addon = models.Addons.get_by_id(int(addon_id))
    screenshot=HttpResponse(addon.screenshot,mimetype='image/jpg')
    screenshot['Content-Disposition'] = 'attachment;filename=%s.jpg' %addon_id
    return screenshot

def viewquote():
    quotations = models1.Quotations.all()
    num = quotations.count()
    quotation = quotations.fetch(1,gen_quote(num))
    quotation = quotation[0].quotation
    return quotation

def viewAddonsBy(request,viewby,value):
    multi_language = memcache.get_multi(['fa_forum_menu_home','fa_forum_menu_dkp','fa_forum_menu_help','fa_forum_menu_admin','fa_forum_view_lang_delete',
                                        'fa_forum_second_menu_topic','fa_forum_second_menu_setting',
                                        'fa_forum_view_lang_edit',
                                        'fa_forum_view_lang_by',
                                        'fa_forum_view_lang_from',
                                        'fa_forum_view_lang_submit',
                                        'fa_forum_sider_lang_home',
                                        'fa_forum_sider_lang_register',
                                        'fa_forum_sider_lang_create_topic',
                                        'fa_forum_sider_lang_member_info',
                                        'fa_forum_sider_lang_signout',
                                        'fa_forum_sider_lang_signin'])
    greeting = LoginUrl()
    user = users.get_current_user()
    keyname = "keytags"
    alltags = models.Tags.get_or_insert(keyname)
    addons = []
    if viewby == 'catagory':
        addons = models.Addons.all().filter('catagory =', value.decode('utf-8'))
    elif viewby == 'author':
        addons = models.Addons.all().filter('auhtor =', value.decode('utf-8'))
    elif viewby == 'tag':
        for addon in models.Addons.all().order('-post_on'):
            if value.decode('utf-8') in addon.tags:
                addons.append(addon)
    payload = dict(quotation=viewquote(),alltags=alltags.alltags,greeting=greeting,addons=addons,user=user,multi_language=multi_language,page_type='addons')
    return render('addons2.html', payload)

@admin_required
def setAddonsTags(request):
    multi_language = memcache.get_multi(['fa_forum_menu_home','fa_forum_menu_dkp','fa_forum_menu_help','fa_forum_menu_admin','fa_forum_view_lang_delete',
                                        'fa_forum_second_menu_topic','fa_forum_second_menu_setting',
                                        'fa_forum_view_lang_edit',
                                        'fa_forum_view_lang_by',
                                        'fa_forum_view_lang_from',
                                        'fa_forum_view_lang_submit',
                                        'fa_forum_sider_lang_home',
                                        'fa_forum_sider_lang_register',
                                        'fa_forum_sider_lang_create_topic',
                                        'fa_forum_sider_lang_member_info',
                                        'fa_forum_sider_lang_signout',
                                        'fa_forum_sider_lang_signin'])
    greeting = LoginUrl()
    user = users.get_current_user()
    keyname = "keytags"
    alltags = models.Tags.get_or_insert(keyname)
    tags = ",".join(["%s" %i for i in alltags.alltags])
    if request.method == 'POST':
        etags = request.POST.get('etags').decode('utf-8')
        alltags.alltags = split_tags(etags)
        alltags.put()
        return HttpResponseRedirect('/addon/tags/edit/')
    payload = dict(quotation=viewquote(),tags=tags,alltags=alltags.alltags,greeting=greeting,user=user,multi_language=multi_language,page_type='addons')
    return render('editstags.html', payload)